use bytes::{BufMut, Bytes, BytesMut};

use super::fingerprint::FingerPrintConfig;

#[derive(Debug, Clone)]
pub struct Chunk {
    data:Bytes,
    fingerprint:Vec<u8>,
}

impl Chunk {
    pub fn from_bytes(data:Bytes) -> Self {
        Self {
            data,
            fingerprint:Vec::new(),
        }
    }

    pub fn from_bytes_mut(data:BytesMut) -> Self {
        Self {
            data:data.freeze(),
            fingerprint:Vec::new(),
        }
    }

    pub fn from_slice(data:&[u8]) -> Self {
        let mut bytes = BytesMut::with_capacity(data.len());
        bytes.put_slice(&data);
        Self {
            data:bytes.freeze(),
            fingerprint:Vec::new(),
        }
    }

    pub fn hash(&mut self, hasher:FingerPrintConfig) {
        self.fingerprint.resize(hasher.fp_len(), 0);
        hasher.digest(&self.data, &mut self.fingerprint);
    }

    pub fn fp(&self) -> &[u8] {
        &self.fingerprint
    }
}